package com.haierp.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.google.common.collect.Maps;
import com.haierp.mapper.PurchaseStorageDetailMapper;
import com.haierp.mapper.TaskDailyOrderHistoryMapper;
import com.haierp.mapper.TaskDailyOrderMapper;
import com.haierp.model.ItemSku;
import com.haierp.model.TaskDailyOrder;
import com.haierp.model.TaskDailyOrderHistory;
import com.haierp.model.TaskDailyOrderUncomplete;
import com.haierp.model.purchase.PurchaseStorageDetail;
import com.haierp.service.ITaskDailyOrderHistoryService;
import com.haierp.service.ITaskDailyOrderService;
import com.haierp.service.ITaskDailyOrderUncompleteService;
import com.haierp.service.purchase.IPurchaseStorageService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.ItemSkuQueryVO;
import com.wangzhixuan.commons.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TaskDailyOrderHistoryServiceImpl extends SuperServiceImpl<TaskDailyOrderHistoryMapper, TaskDailyOrderHistory>  implements ITaskDailyOrderHistoryService {

    @Autowired
    private ITaskDailyOrderUncompleteService taskDailyOrderUncompleteService;

    @Autowired
    private PurchaseStorageDetailMapper purchaseStorageDetailMapper;

    @Override
    public void addNocomplestTaskDailyOrder() {
        List<TaskDailyOrderHistory> taskDailyOrderHistoryList = this.baseMapper.queryYesterdayTaskDailyOrder();
        List<PurchaseStorageDetail> purchaseStorageDetails = purchaseStorageDetailMapper.queryYesterdayPurStoDetail();
        Map<Long, Integer> taskDailyOrderHistoryMap = Maps.newHashMap();
        purchaseStorageDetails.forEach(purchaseStorageDetail -> {
            taskDailyOrderHistoryMap.put(purchaseStorageDetail.getSkuId(), purchaseStorageDetail.getQuantity());
        });

        List<TaskDailyOrderUncomplete> nocompleteTaskDailyOrderList = new ArrayList<>();
        for (TaskDailyOrderHistory taskDailyOrderHistory : taskDailyOrderHistoryList) {
            Integer readyPurchaseQuantity = taskDailyOrderHistoryMap.get(taskDailyOrderHistory.getSkuId());
            int noCompletePurchaseNeed = 0;
            if(readyPurchaseQuantity == null) {
                noCompletePurchaseNeed = taskDailyOrderHistory.getPurchaseNeed();
            } else {
                noCompletePurchaseNeed = taskDailyOrderHistory.getPurchaseNeed() - readyPurchaseQuantity;
            }

            if(noCompletePurchaseNeed > 0) {
                TaskDailyOrderUncomplete nocompleteTaskDailyOrder = new TaskDailyOrderUncomplete();
                BeanUtils.copyProperties(taskDailyOrderHistory, nocompleteTaskDailyOrder);
                nocompleteTaskDailyOrder.setId(null);
                nocompleteTaskDailyOrder.setPurchaseNeed(noCompletePurchaseNeed);
                nocompleteTaskDailyOrder.setGmtCreate(new Date());
                nocompleteTaskDailyOrder.setGmtModify(new Date());
                nocompleteTaskDailyOrderList.add(nocompleteTaskDailyOrder);
            }
        }
        taskDailyOrderUncompleteService.insertBatch(nocompleteTaskDailyOrderList);
    }
}
